<template>
  <div class="app-container">
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <el-button type="primary" v-hasPermi="['sys_dept_add']" class="el-icon-plus" @click="add">新增</el-button>
      <el-button type="danger" class="el-icon-delete" @click="cleanCache">更新缓存</el-button>
      <right-toolbar :haveSearch.sync="haveSearch" @queryTable="getList"></right-toolbar>
    </div>
    <el-table :data="deptData" v-loading.body="listLoading" border fit highlight-current-row class="table-list" :row-key="getRowKeys" :expand-row-keys="expandRow">
      <el-table-column prop="deptName" label="部门名称" align="left">
      </el-table-column>
      <el-table-column prop="deptCode" label="部门编码" align="center" width="200">
      </el-table-column>
      <el-table-column prop="deptType" label="部门类型" align="center" width="100">
          <template slot-scope="scope">
              <el-tag v-if="scope.row.deptType==0" type="success">公司</el-tag>
              <el-tag v-if="scope.row.deptType==1">部门</el-tag>
              <el-tag v-if="scope.row.deptType==2" type="info">小组</el-tag>
            </template>
      </el-table-column>
      <el-table-column prop="orderNum" label="排序" align="center" width="100"></el-table-column>
      <el-table-column align="center" label="修改时间" prop="updateTime" width="150px"></el-table-column>
      <el-table-column align="center" label="操作"  width="160" fixed="right">
        <template slot-scope="scope">
          <el-button plain v-hasPermi="['sys_dept_edit']" type="violet" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button plain v-hasPermi="['sys_dept_remove']" type="danger" @click="removeByIds(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>

    <el-dialog title="编辑" :width="'520px'" :visible.sync="isShowEdit" :close-on-click-modal="false">
      <el-form :rules="rules" ref="saveDeptData" :model="saveDeptData" label-width="80px">
        <el-form-item label="上级部门" prop="parentId">
          <treeselect v-model="saveDeptData.parentId" :multiple="false" :options="deptData" :load-options="loadOptions" :default-expand-level="2" placeholder="请选择上级部门" @select="selectParentDept"/>
        </el-form-item>
        <el-form-item label="部门类型" prop="deptType">
           <el-select v-model="saveDeptData.deptType" placeholder="请选择部门类型" style="width:400px">
              <el-option label="公司" value='0' ></el-option>
              <el-option label="部门" value='1' ></el-option>
              <el-option label="小组" value='2' ></el-option>
           </el-select>
        </el-form-item>
        <el-form-item label="部门名称" prop="deptName">
          <el-input v-model="saveDeptData.deptName" style="width: 400px;" placeholder="请输入部门名称" maxlength="20"></el-input>
        </el-form-item>
        <el-form-item label="部门编码" prop="deptCode">
          <el-input v-model="saveDeptData.deptCode" style="width: 400px;" placeholder="请输入部门编码" maxlength="10"></el-input>
        </el-form-item>
        <el-form-item label="排序" prop="orderNum">
          <el-input-number v-model="saveDeptData.orderNum" :min="0" :max="999999" style="width: 100%;"></el-input-number>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isShowEdit = false">取 消</el-button>
        <el-button type="primary" @click="onSave('saveDeptData')">确 定</el-button>
      </div>
    </el-dialog>
    </div>
</template>

<script>
import * as dept from './js/dept'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { mapGetters } from 'vuex'

export default {
  name: 'deptList',
  components: { Treeselect },
  data() {
    return {
      deptData: [],
      expandRow: [],
      listLoading: true,
      // 是否有搜索条件
      haveSearch: false,
      total: 0,
      isShowEdit: false,
      saveDeptData: {
        id: '',
        deptName: '',
        parentId: '',
        deptType: '',
        orderNum: '',
        grandParentIds: '',
        deptCode: '',
        status: 0
      },
      getRowKeys(row) {
        return row.id
      },
      rules: {
        deptName: [
          { required: true, message: '请输入部门名称', trigger: 'blur' }
        ],
        deptType: [
          { required: true, message: '请选择部门类型', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  computed: {
    ...mapGetters(['permissions'])
  },
  methods: {
    loadOptions() {},
    clickTable(row, index, e) {
      this.$refs.refTable.toggleRowExpansion(row)
    },
    getList() {
      this.listLoading = true
      dept.list().then(response => {
        this.deptData = response.data
        this.listLoading = false
        this.expandRow.push(this.deptData[0].id)
      })
    },
    // 搜索
    handleSearch(index, row) {
      this.getList()
    },
    // 删除
    removeByIds(id) {
      this.$confirm('请确定是否删除该部门?', '提示', {
        confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning'
      }).then(() => {
        dept.removeByIds(id).then((response) => {
          this.$message({ type: 'success', message: '操作成功!' })
          this.getList()
        })
      }).catch(function(error) { console.log(error) })
    },
    // 新增
    add(pit, index) {
      this.isShowEdit = true
      // 清空表单数据
      this.$nextTick(() => {
        this.resetForm('saveDeptData')
        this.saveDeptData.id = null
        this.saveDeptData.parentId = null
        this.saveDeptData.deptName = ''
        this.saveDeptData.deptType = '0'
        this.saveDeptData.orderNum = null
        this.saveDeptData.deptCode = ''
      })
    },
    // 编辑
    handleEdit(index, row) {
      this.isShowEdit = true
      // 设置表单数据
      this.$nextTick(() => {
        this.resetForm('saveDeptData')
        this.saveDeptData.id = row.id
        this.saveDeptData.parentId = row.parentId === 0 ? null : row.parentId
        this.saveDeptData.grandParentIds = row.grandParentIds
        this.saveDeptData.deptName = row.deptName
        this.saveDeptData.deptCode = row.deptCode
        this.saveDeptData.deptType = row.deptType + ''
        this.saveDeptData.orderNum = row.orderNum
      })
    },
    // 清理缓存
    cleanCache(){
      dept.cleanCache().then((response) => {
        this.$message({type: 'success',message: '操作成功!'})
        this.getList()
      }).catch(function(error) {
            console.log(error)
      })
    },
    // 保存
    onSave(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.isShowEdit = false
          dept.save(this.saveDeptData).then((response) => {
            this.$message({ type: 'success', message: '操作成功!' })
            this.getList()
          }).catch(function(error) {
            console.log(error)
          })
        }
      })
    },
    selectParentDept(v) {
      if (v.grandParentIds) {
        this.saveDeptData.grandParentIds = v.grandParentIds + ',' + v.id
      } else {
        this.saveDeptData.grandParentIds = v.id
      }
    }
  }
}
</script>
